package io.renren.modules.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.base.annotation.Login;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.entity.DetailEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;

import io.renren.modules.entity.MessageEntity;
import io.renren.modules.service.MessageService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户留言表
 *
 * @author xiaozhao
 * @email langzi2582@163.com
 * @date 2019-01-05 00:31:21
 */
@RestController
@RequestMapping("api")
@Api(tags = "用户留言表接口")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 列表
     */
    @Login
    @PostMapping("message/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = messageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */

    @Login
    @PostMapping("message/info/{id}")
    @ApiOperation("信息")

    public R info(@PathVariable("id") Integer id) {
        MessageEntity message = messageService.selectById(id);

        return R.ok().put("message", message);
    }

    /**
     * 保存
     */

    @Login
    @PostMapping("message/save")
    @ApiOperation("保存")

    public R save(@RequestBody MessageEntity message) {
        messageService.insert(message);

        return R.ok();
    }

    @PostMapping("message/UploadVoice")
    @ApiOperation("上传录音")
    public R UploadFile(HttpServletRequest req, MultipartHttpServletRequest multiReq) {

        String voiceFilePath = multiReq.getFile("userVoice").getOriginalFilename();
        String voiceFileSuffix = voiceFilePath.substring(voiceFilePath.indexOf('.') + 1, voiceFilePath.length());
        System.out.println(voiceFilePath+voiceFileSuffix);
        FileOutputStream voiceFos = null;
        FileInputStream voiceFis = null;

        try {
            String path = "D://Program Files//apache-tomcat-8.0.3//webapps//uploadFiles1//";
            File file = new File(path);
            if (file == null || !file.exists()) {
                file.mkdirs();
            }
            long timestamp = System.currentTimeMillis();
//上传的录音
            String voicePath = null;
            if (voiceFileSuffix.equals("arm")) {
                voiceFis = (FileInputStream) multiReq.getFile("userVoice").getInputStream();
                voiceFos = new FileOutputStream(file + "/" + timestamp + "." + voiceFileSuffix);
                byte[] voiceTemp = new byte[1024];
                int j = voiceFis.read(voiceTemp);
                while (j != -1) {
                    voiceFos.write(voiceTemp, 0, voiceTemp.length);
                    voiceFos.flush();
                    j = voiceFis.read(voiceTemp);
                }
                voicePath = "uploadFiles1/" + +timestamp + "." + voiceFileSuffix;
            }

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String userCreateTime = df.format(new Date());
            MessageEntity messageEntity = new MessageEntity();

            //
            messageEntity.setExpertId(req.getParameter("expertId"));
            messageEntity.setUserMessage(req.getParameter("userMessage"));
            messageEntity.setUserVoice(voicePath);
            messageEntity.setUserCreateTime(userCreateTime);
            messageEntity.setUserId(req.getParameter("userId"));
            messageService.insert(messageEntity);
            return R.ok();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {


            if (voiceFis != null) {
                try {

                    voiceFis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (voiceFos != null) {
                try {

                    voiceFos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        http:
//10.129.50.212/jpg/
        return null;
    }

    /**
     * 修改
     */
    @Login
    @PostMapping("message/update")
    @ApiOperation("修改")

    public R update(@RequestBody MessageEntity message) {
        ValidatorUtils.validateEntity(message);
        messageService.updateAllColumnById(message);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @PostMapping("message/delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids) {
        messageService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
